package db_util

import (
	"app/app_src/framework/db/orm"

	_ "github.com/go-sql-driver/mysql"
	"github.com/jmoiron/sqlx"
)

type UtilContext struct {
	DriverName     string
	DataSourceName string
}

// 默认驱动名
const _DEFAULT_DRIVER_NAME = "mysql"

var _UtilContext UtilContext

func InitUtilContext(utilContext *UtilContext) {
	if utilContext != nil {
		_UtilContext = *utilContext
	}

	driverName := _UtilContext.DriverName
	if driverName == "" {
		_UtilContext.DriverName = _DEFAULT_DRIVER_NAME
	}
}

func NewUtilContext(dataSourceName string) *UtilContext {
	var newContext UtilContext
	newContext.DataSourceName = dataSourceName

	return &newContext
}

// 获取数据库对象
func MustGetDB() *orm.DB {
	db := sqlx.MustOpen(_UtilContext.DriverName, _UtilContext.DataSourceName)

	return orm.NewDB(db)
}
